package com.sangmo.fts.api.teacher;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.basic.model.FileUpload;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.teacher.vo.ExaminationDistributionView;
import com.sangmo.fts.teacher.interfaces.ExaminationSubmitRequest;
import com.sangmo.fts.teacher.interfaces.ExaminationEnterRequest;
import com.sangmo.fts.teacher.service.ExaminationService;
import com.sangmo.fts.teacher.interfaces.ExaminationCreation;
import com.sangmo.fts.teacher.interfaces.ExaminationUpdate;
import com.sangmo.fts.teacher.model.Examination;
import com.sangmo.fts.training.model.Training;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@TrainingApi
@Controller
@RequestMapping("/examination/v1")
@Access("examination")
public class ExaminationApi {

    @Autowired
    private ExaminationService examinationService;

    @Access(":create")
    @PostMapping(value = "/create")
    public @ResponseBody
    ApiResponse<Examination> create(@RequestBody @Valid ExaminationCreation obj) {
        return ApiResponse.make(examinationService.create(obj));
    }

    @Access(":read")
    @PostMapping("/search")
    public @ResponseBody ApiResponse<PageResult<Examination>> search(@RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("Examination.search", 1);
        return ApiResponse.make(examinationService.search(ps));
    }

    @Access(":read")
    @PostMapping("/{id}/read")
    public @ResponseBody ApiResponse<Examination> read(@PathVariable("id") Long id) {
        ExtViewHelper.setGroup("Examination.detail", 1);
        return ApiResponse.make(examinationService.find(id));
    }

    @Access(":update")
    @PostMapping("/{id}/update")
    public @ResponseBody ApiResponse<Examination> update(@PathVariable("id") Long id, @RequestBody @Valid ExaminationUpdate co) {
        return ApiResponse.make(examinationService.update(id, co));
    }

    @Access(":remove")
    @PostMapping("/{id}/remove")
    public @ResponseBody ApiResponse remove(@PathVariable("id") Long id) {
        examinationService.deleteById(id);
        return ApiResponse.make();
    }

    /*@PostMapping(value = "/{id}/publish")
    public @ResponseBody
    ApiResponse publish(@PathVariable("id")Long id) {
        return ApiResponse.make(examinationService.publish(id));
    }*/

    /** 进入考核 **/
    @Access(":enter")
    @PostMapping(value = "/{id}/enter")
    public @ResponseBody
    ApiResponse<Training> enter(@PathVariable("id") Long id, @RequestBody @Valid ExaminationEnterRequest request) {

        return ApiResponse.make(examinationService.enter(id, request));
    }

    @Access(":upload")
    @PostMapping(value = "/{id}/{trainingId}/document/upload")
    public @ResponseBody
    ApiResponse<FileUpload> upload(@PathVariable("id") Long id, @PathVariable("trainingId") Long trainingId, @RequestBody @Valid ExaminationSubmitRequest request) {
        return ApiResponse.make(examinationService.createReportDocument(id, trainingId, request));
    }

    @Access(":submit")
    @PostMapping(value = "/{id}/{trainingId}/submit")
    public @ResponseBody
    ApiResponse submit(@PathVariable("id") Long id, @PathVariable("trainingId") Long trainingId) {
        examinationService.submit(id, trainingId);
        return ApiResponse.make();
    }

    @PostMapping(value = "/{id}/distribution")
    public @ResponseBody
    ApiResponse<List<ExaminationDistributionView>> distribution(@PathVariable("id") Long id) {
        return ApiResponse.make(examinationService.distribution(id));
    }

}
